<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="申请人" prop="fromPartyId">
        <el-select v-model="queryParams.fromPartyId" placeholder="请选择申请人" clearable filterable>
          <el-option v-for="dict in fromPartyIdOptions" :key="dict.partyId" :label="dict.firstName"
            :value="dict.partyId" />
        </el-select>
      </el-form-item>

      <el-form-item label="请求名称" prop="custRequestName">
        <el-input v-model="queryParams.custRequestName" placeholder="请输入请求名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="状态" prop="statusId">
        <el-select v-model="queryParams.statusId" placeholder="请选择状态" clearable>
          <el-option v-for="dict in statusIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ehr:cust_request:add']">新增客户请求</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleTask"
          v-hasPermi="['ehr:cust_request:edit']">生成任务计划</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ehr:cust_request:edit']">修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ehr:cust_request:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:cust_request:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cust_requestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="请求编号" align="center" prop="custRequestId" width="80" />
      <el-table-column label="申请人" align="center" prop="fromPartyId" :formatter="fromPartyIdFormat" />
      <el-table-column label="请求日期" align="center" prop="custRequestDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.custRequestDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求名称" align="center" prop="custRequestName" />
      <el-table-column label="优先级" align="center" prop="priority" width="60" :formatter="priorityFormat" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="状态" align="center" prop="statusId" width="80" :formatter="statusIdFormat" />
      <el-table-column label="创建人" align="center" prop="createdByUserLogin" width="80"
        :formatter="createdByUserLoginFormat" />
      <el-table-column label="创建时间" align="center" prop="createdStamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:cust_request:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:cust_request:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:cust_request:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户请求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人" prop="fromPartyId">
              <el-select v-model="form.fromPartyId" placeholder="请选择申请人" filterable>
                <el-option v-for="dict in fromPartyIdOptions" :key="dict.partyId" :label="dict.firstName"
                  :value="dict.partyId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求日期" prop="custRequestDate">
              <el-date-picker clearable v-model="form.custRequestDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择请求日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="请求名称" prop="custRequestName">
              <el-input v-model="form.custRequestName" placeholder="请输入请求名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优先级" prop="priority">
              <el-select v-model="form.priority" placeholder="请选择优先级">
                <el-option v-for="dict in priorityOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="响应日期" prop="responseRequiredDate">
              <el-date-picker clearable v-model="form.responseRequiredDate" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" placeholder="请选择响应日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="openDateTime">
              <el-date-picker clearable v-model="form.openDateTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="closedDateTime">
              <el-date-picker clearable v-model="form.closedDateTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="创建人" prop="createdByUserLogin">
              <el-select v-model="form.createdByUserLogin" placeholder="请选择创建人" filterable>
                <el-option v-for="dict in createdByUserLoginOptions" :key="dict.partyId" :label="dict.firstName"
                  :value="dict.partyId"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="状态" prop="statusId">
              <el-select v-model="form.statusId" placeholder="请选择状态">
                <el-option v-for="dict in statusIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCust_request, getCust_request, delCust_request, addCust_request, updateCust_request } from "@/api/ehr/cust_request";

export default {
  name: "Cust_request",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户请求表格数据
      cust_requestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 请求类型业务字典
      custRequestTypeIdOptions: [],
      // 请求分类业务字典
      custRequestCategoryIdOptions: [],
      // 状态业务字典
      statusIdOptions: [],
      // 申请人业务字典
      fromPartyIdOptions: [],
      // 优先级业务字典
      priorityOptions: [],
      // 创建人业务字典
      createdByUserLoginOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        custRequestId: null,
        custRequestTypeId: null,
        custRequestCategoryId: null,
        statusId: null,
        fromPartyId: null,
        custRequestDate: null,
        responseRequiredDate: null,
        custRequestName: null,
        openDateTime: null,
        closedDateTime: null,
        createdByUserLogin: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fromPartyId: [
          { required: true, message: "申请人不能为空", trigger: "change" }
        ],
        custRequestDate: [
          { required: true, message: "请求日期不能为空", trigger: "blur" }
        ],
        custRequestName: [
          { required: true, message: "请求名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // 状态业务字典
    this.getEnumDicts("CUST_REQUEST_STATUS").then((response) => {
      this.statusIdOptions = response.data;
    });
    // 申请人业务字典
    this.getEntityDicts("Person:partyId:firstName").then((response) => {
      this.fromPartyIdOptions = response.data;
    });
    // 优先级业务字典
    this.getEnumDicts("PRIORITY").then((response) => {
      this.priorityOptions = response.data;
    });
    // 创建人业务字典
    this.getEntityDicts("Person:partyId:firstName").then((response) => {
      this.createdByUserLoginOptions = response.data;
    });
    this.getList();
  },
  methods: {
    statusIdFormat(row, column) {
      let val = null;
      this.statusIdOptions.forEach((item, index) => {
        if (row.statusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    fromPartyIdFormat(row, column) {
      let val = null;
      this.fromPartyIdOptions.forEach((item, index) => {
        if (row.fromPartyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    priorityFormat(row, column) {
      let val = null;
      this.priorityOptions.forEach((item, index) => {
        if (row.priority === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    createdByUserLoginFormat(row, column) {
      let val = null;
      this.createdByUserLoginOptions.forEach((item, index) => {
        if (row.createdByUserLogin === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    /** 查询客户请求列表 */
    getList() {
      this.loading = true;
      listCust_request(this.queryParams).then(response => {
        this.cust_requestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        custRequestId: null,
        custRequestTypeId: null,
        custRequestCategoryId: null,
        statusId: null,
        fromPartyId: null,
        priority: null,
        custRequestDate: null,
        responseRequiredDate: null,
        custRequestName: null,
        description: null,
        maximumAmountUomId: null,
        productStoreId: null,
        salesChannelEnumId: null,
        fulfillContactMechId: null,
        currencyUomId: null,
        openDateTime: null,
        closedDateTime: null,
        internalComment: null,
        reason: null,
        createdDate: null,
        createdByUserLogin: null,
        lastModifiedDate: null,
        lastModifiedByUserLogin: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.custRequestId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加客户请求";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const custRequestId = row.custRequestId || this.ids
      getCust_request(custRequestId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "客户请求详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const custRequestId = row.custRequestId || this.ids
      getCust_request(custRequestId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户请求";
      });
    },

    handleTask(row) {
      const custRequestId = row.custRequestId || this.ids[0]
      // console.info("handleAssign query : " + workEffortId)
      var _path = "/cust/request/task/";
      this.$router.push({
        path: _path,
        query: {
          custRequestId: custRequestId,
        },
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.custRequestId != null && this.updateFlag) {
            updateCust_request(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCust_request(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const custRequestIds = row.custRequestId || this.ids;
      this.$modal.confirm('是否确认删除客户请求编号为"' + custRequestIds + '"的数据项？').then(function () {
        return delCust_request(custRequestIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/cust_request/export', {
        ...this.queryParams
      }, `cust_request_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
